﻿using System;
using System.Linq;
using System.Web.UI;
using BookingEngine.Data;

namespace BookingEngine.Web.cn
{
    public partial class SearchResv : Page
    {
        #region 绑定数据

        private void BindData(HotelGetResvResponse response)
        {
            confnumContent.Text = response.Confnum;
            bookDateContent.Text = response.BookDate.ToString("yyyy年MM月dd日 HH:mm:ss");
            bookStatusContent.Text = string.Format("{0}({1})", response.BookingStatus, response.SignStatus);

            checkInDateContent.Text = response.CheckInDate.ToString("yyyy年MM月dd日");
            checkOutDateContent.Text = response.CheckOutDate.ToString("yyyy年MM月dd日");
            roomsNumberContent.Text = string.Format("{0}间", response.Rooms);
            adultsNumberContent.Text = string.Format("{0}人", response.Adults);
            childrenNumberContent.Text = string.Format("{0}人", response.Children);

            totalPriceContent.Text = response.TotalPrice.ToString("#,##0.00");

            guestNameContent.Text = string.Format("{0}{1}", response.LastName, response.FirstName);
            guestMobileContent.Text = response.Mobile;
            guestPhoneContent.Text = response.Phone;
            guestEmailContent.Text = response.Email;

            hotelTitleContent.Text = response.HotelName;
            hotelNameContent.Text = response.HotelName;
            hotelCodeContent.Text = response.HotelCode;
            roomNameContent.Text = response.RoomType;
            ratePlanContent.Text = response.RatePlan;
            guaranteeRuleContent.Text = response.GuaranteeRule ?? "(无)";
            cancellationRuleContent.Text = response.CancellationRule ?? "(无)";

            ratesContent.Text = response.Rates.Aggregate("", (s, rate) => string.Format("{0}；{1} : 价格:{2}", s, rate.Item1.ToString("yyyy-MM-dd"), rate.Item2.ToString("#,##0.00"))).Trim('；');
        }

        #endregion

        #region 事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(confnumSearchTextBox.Text))
            {
                try
                {
                    var responseXml = HotelReservationService.GetResv("zh", confnumSearchTextBox.Text.Trim());
                    var response = new HotelGetResvResponse(responseXml);
                    if (response.IsSuccess && !string.IsNullOrEmpty(response.Confnum))
                    {
                        questionPanel.Visible = false;
                        nothingPanel.Visible = false;
                        resultPanel.Visible = true;
                        cancelButton.Visible = true;
                        cancelResultPanel.Visible = false;
                        BindData(response);
                    }
                    else
                    {
                        questionPanel.Visible = false;
                        nothingPanel.Visible = true;
                        resultPanel.Visible = false;
                        cancelButton.Visible = false;
                        cancelResultPanel.Visible = false;
                    }
                }
                catch (Exception)
                {
                    questionPanel.Visible = false;
                    nothingPanel.Visible = true;
                    resultPanel.Visible = false;
                    cancelButton.Visible = false;
                    cancelResultPanel.Visible = false;
                }
            }
            else
            {
                questionPanel.Visible = true;
                nothingPanel.Visible = false;
                resultPanel.Visible = false;
                cancelButton.Visible = false;
                cancelResultPanel.Visible = false;
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hotelCodeContent.Text))
            {
                try
                {
                    var responseXml = HotelReservationService.CancelResv("zh", hotelCodeContent.Text, confnumContent.Text);
                    var response = new HotelCancelResvResponse(responseXml);
                    if (response.IsSuccess)
                    {
                        questionPanel.Visible = false;
                        nothingPanel.Visible = false;
                        resultPanel.Visible = true;
                        cancelButton.Visible = true;
                        cancelResultPanel.Visible = true;
                        cancelMessageLabel.Text = "取消成功 ! ";
                    }
                    else
                    {
                        questionPanel.Visible = false;
                        nothingPanel.Visible = true;
                        resultPanel.Visible = false;
                        cancelButton.Visible = false;
                        cancelResultPanel.Visible = true;
                        cancelMessageLabel.Text = "取消失败 ! ";
                    }
                }
                catch (Exception)
                {
                    questionPanel.Visible = false;
                    nothingPanel.Visible = true;
                    resultPanel.Visible = false;
                    cancelButton.Visible = false;
                    cancelResultPanel.Visible = true;
                    cancelMessageLabel.Text = "取消失败 !";
                }
            }
            else
            {
                questionPanel.Visible = true;
                nothingPanel.Visible = false;
                resultPanel.Visible = false;
                cancelButton.Visible = false;
                cancelResultPanel.Visible = true;
                cancelMessageLabel.Text = "取消失败 ! 无法找到酒店编号！";
            }
        }

        #endregion
    }
}